﻿
package kliment.display.filters.noise {
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.display.DisplayObject;
	import flash.filters.ColorMatrixFilter;
	import kliment.display.filters.noise.NoiseMatrix;
	import kliment.display.filters.ImageFilter;
	import kliment.display.filters.ImageFilterEvents;
	
	/**
	* Manipulator for add noises on the Bitmap
	* 
	* @author Kliment
	*/
	public class NoiseManipulator extends EventDispatcher {
		private var _bitmap:DisplayObject;
		
		private var _glass:Number = 0;
		private var _popArt:Number = 0;
		private var _sponge:Number = 0;
		private var _blur:Number = 0;
		private var _edges:Boolean = false;
		
		private var _noiseMatrix:NoiseMatrix;
		private var _host:ImageFilter;
		
		/**
		 * Constructor
		 * 
		 * @param	displayObject Target Bitmap
		 * @param	_host Parent ImageFilter class
		 */
		public function NoiseManipulator(bitmap:DisplayObject, host:ImageFilter):void {
			_bitmap = bitmap;
			this._host = host;
		}
		
		/**
		 * @param glass, blur, edges = 0..100
		 */
		public function set glass(value:Number):void {
			_glass = value;
			updateNoise();
		}
		
		public function get glass():Number {
			return _glass;
		}
		
		public function set popArt(value:Number):void {
			_popArt = value;
			updateNoise();
		}
		
		public function get popArt():Number {
			return _popArt;
		}
		
		public function set blur(value:Number):void {
			_blur = value;
			updateNoise();
		}
		
		public function get blur():Number {
			return _blur;
		}
		
		public function set edges(value:Boolean) {
			_edges = value;
			updateNoise();
		}
		
		public function get edges():Boolean {
			return _edges;
		}
		
		public function get noiseMatrix():Array {
			if (!_noiseMatrix)
				return new Array();
			return _noiseMatrix;
		}
		
		private function updateNoise():void {
			dispatchEvent(new Event(Event.COMPLETE));
			_noiseMatrix = new NoiseMatrix(_bitmap);
			_noiseMatrix.glass(_glass);
			//_noiseMatrix.popArt(_popArt);
			_noiseMatrix.edges(_edges);
			_noiseMatrix.blur(_blur);
			
			var filters:Array = [new ColorMatrixFilter(_host.colorMatrix)];
			filters = filters.concat(_noiseMatrix);
			
			_bitmap.filters = filters;
			
			dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}
